package org.rich.charlesmurphy.ui;

import static org.rich.charlesmurphy.core.CMIds.CACHE;
import static org.rich.charlesmurphy.core.CMIds.CSS;
import static org.rich.charlesmurphy.core.CMIds.IMAGES;
import static org.rich.charlesmurphy.core.CMIds.JS;
import static org.rich.charlesmurphy.core.CMIds.METHOD;
import static org.rich.charlesmurphy.core.CMIds.OTHER;
import static org.rich.charlesmurphy.core.CMIds.PATH;
import static org.rich.charlesmurphy.core.CMIds.REMOTE_HOST;
import static org.rich.charlesmurphy.core.CMIds.REQUESTS;
import static org.rich.charlesmurphy.core.CMIds.RESPONSE_SIZE;
import static org.rich.charlesmurphy.core.CMIds.SUCCESS;
import static org.rich.charlesmurphy.core.CMIds.TIME;
import static org.rich.charlesmurphy.core.CMIds.TOTAL_PAGE_TIME;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import org.ddevil.data.set.BeanDataSet;
import org.ddevil.data.set.DataSet;
import org.rich.charlesmurphy.core.CMIds;
import org.rich.charlesmurphy.model.Request;
import org.rich.charlesmurphy.model.RequestGroup;
import org.rich.charlesmurphy.ui.renderers.BarRenderer;
import org.rich.charlesmurphy.ui.renderers.UrlRenderer;
import org.windowkit.toolkit.widget.table.RecordTablePane;
import org.windowkit.toolkit.widget.table.render.LongRenderer;

/**
 *
 * @author Rich
 */
public class ViewRequestGroups extends AbstractTableView {
	private static final long serialVersionUID = -2626910460361328946L;

	private static String[] IDS = new String[]{
		REMOTE_HOST,
		METHOD,
		PATH,
		RESPONSE_SIZE,
		REQUESTS,
		SUCCESS,
		CACHE,
		CSS,
		JS,
		IMAGES,
		OTHER,
		TOTAL_PAGE_TIME,
		TIME
	};

	static int common = 65;
	static int[] COLUMN_WIDTHS = new int[]{
			85,
			40,
			165,
			70,
			common,
			common,
			common,
			common,
			common,
			common,
			common,
			60,
			110
	};

	public ViewRequestGroups(String title) {
		super(title + " [Request Groups]");
	}

	public ViewRequestGroups(String title, DataSet<RequestGroup> model) {
		this(title);
		setModel(model);
	}

	@Override
	protected String[] getColumnIds() {
		return IDS;
	}

	public void setModel(DataSet<RequestGroup> data) {
		getTable().setData(data);
	}

	@Override
	protected void initComponents() {
		super.initComponents();
		RecordTablePane tbl = getTable();
		//TODO bleh...
		tbl.setColumnWidths(COLUMN_WIDTHS);
		tbl.setColumnCellRenderer(CMIds.PATH, new UrlRenderer());
		tbl.setColumnCellRenderer(CMIds.REQUESTS, new BarRenderer(Colors.REQUESTS));
		tbl.setColumnCellRenderer(CMIds.SUCCESS, new BarRenderer(Colors.SUCCSSS));
		tbl.setColumnCellRenderer(CMIds.CACHE, new BarRenderer(Colors.CACHED));
		tbl.setColumnCellRenderer(CMIds.CSS, new BarRenderer(Colors.CSS));
		tbl.setColumnCellRenderer(CMIds.JS, new BarRenderer(Colors.JS));
		tbl.setColumnCellRenderer(CMIds.IMAGES, new BarRenderer(Colors.IMAGE));
		tbl.setColumnCellRenderer(CMIds.OTHER, new BarRenderer(Colors.OTHER));
//		tbl.setColumnCellRenderer(CMIds.AJAX, new BarRenderer(Colors.AJAX));

		tbl.setColumnCellRenderer(RESPONSE_SIZE, new LongRenderer());  //TODO investigate why this isnt automatic
		tbl.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					RequestGroup rg = (RequestGroup) getTable().getSelectedRecord();

					ViewByRequest newWindow = new ViewByRequest("All requests for " + rg.getPath());
					newWindow.setModel(new BeanDataSet<Request>(rg.getChildren()));
					getLauncher().launchWindow(newWindow);
				}

			}
		});
	}

}
